<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 1.typeof 判断7种
      //  判断基础数据类型null除外
      console.log(typeof 1);
      console.log(typeof "1");
      console.log(typeof undefined);
      console.log(typeof null); // object
      console.log(typeof true);
      console.log(typeof BigInt(9007199254740991));
      console.log(typeof Symbol(2));

      console.log(typeof undefined === "undefined");
      console.log(typeof null === "object");
      console.log("======================");

      // 2. instanceof
      // 判断以用数据类型，无法判断基础数据类型
      let a = [1, 2, 3];
      console.log(a instanceof Array);

      let b = { a: 1, b: "2" };
      console.log(b instanceof Map);

      let Constr = function () {};
      let c = new Constr();
      console.log(c instanceof Constr);

      console.log(typeof a);
      console.log(typeof b);
      console.log(typeof c);

      // 检查类型
      function myCheckType(o) {
        let type = typeof o;
        if (type !== "object") {
          return type;
        }

        return Object.prototype.toString
          .call(o)
          .replace(/^\[object (\S+)\]$/, "$1");
      }

      console.log(myCheckType(a));
      console.log(myCheckType(b));
      console.log(myCheckType(c));
      console.log(myCheckType(Constr));
      console.log(myCheckType(new RegExp("ab+c")));
      // console.log(myCheckType(\123\));
    </script>
  </body>
</html>
